package com.sjgs.gis.service;

import java.util.List;

import javax.persistence.criteria.JoinType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import io.github.jhipster.service.QueryService;

import com.sjgs.gis.domain.JdbcCluster;
import com.sjgs.gis.domain.*; // for static metamodels
import com.sjgs.gis.repository.JdbcClusterRepository;
import com.sjgs.gis.service.dto.JdbcClusterCriteria;
import com.sjgs.gis.service.dto.JdbcClusterDTO;
import com.sjgs.gis.service.mapper.JdbcClusterMapper;

/**
 * Service for executing complex queries for {@link JdbcCluster} entities in the database.
 * The main input is a {@link JdbcClusterCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link List} of {@link JdbcClusterDTO} or a {@link Page} of {@link JdbcClusterDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class JdbcClusterQueryService extends QueryService<JdbcCluster> {

    private final Logger log = LoggerFactory.getLogger(JdbcClusterQueryService.class);

    private final JdbcClusterRepository jdbcClusterRepository;

    private final JdbcClusterMapper jdbcClusterMapper;

    public JdbcClusterQueryService(JdbcClusterRepository jdbcClusterRepository, JdbcClusterMapper jdbcClusterMapper) {
        this.jdbcClusterRepository = jdbcClusterRepository;
        this.jdbcClusterMapper = jdbcClusterMapper;
    }

    /**
     * Return a {@link List} of {@link JdbcClusterDTO} which matches the criteria from the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public List<JdbcClusterDTO> findByCriteria(JdbcClusterCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<JdbcCluster> specification = createSpecification(criteria);
        return jdbcClusterMapper.toDto(jdbcClusterRepository.findAll(specification));
    }

    /**
     * Return a {@link Page} of {@link JdbcClusterDTO} which matches the criteria from the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @param page The page, which should be returned.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Page<JdbcClusterDTO> findByCriteria(JdbcClusterCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<JdbcCluster> specification = createSpecification(criteria);
        return jdbcClusterRepository.findAll(specification, page)
            .map(jdbcClusterMapper::toDto);
    }

    /**
     * Return the number of matching entities in the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    @Transactional(readOnly = true)
    public long countByCriteria(JdbcClusterCriteria criteria) {
        log.debug("count by criteria : {}", criteria);
        final Specification<JdbcCluster> specification = createSpecification(criteria);
        return jdbcClusterRepository.count(specification);
    }

    /**
     * Function to convert {@link JdbcClusterCriteria} to a {@link Specification}
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching {@link Specification} of the entity.
     */
    protected Specification<JdbcCluster> createSpecification(JdbcClusterCriteria criteria) {
        Specification<JdbcCluster> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getId(), JdbcCluster_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), JdbcCluster_.name));
            }
            if (criteria.getVersion() != null) {
                specification = specification.and(buildStringSpecification(criteria.getVersion(), JdbcCluster_.version));
            }
            if (criteria.getDriver() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDriver(), JdbcCluster_.driver));
            }
            if (criteria.getHost() != null) {
                specification = specification.and(buildStringSpecification(criteria.getHost(), JdbcCluster_.host));
            }
            if (criteria.getPort() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getPort(), JdbcCluster_.port));
            }
            if (criteria.getUsername() != null) {
                specification = specification.and(buildStringSpecification(criteria.getUsername(), JdbcCluster_.username));
            }
            if (criteria.getPassword() != null) {
                specification = specification.and(buildStringSpecification(criteria.getPassword(), JdbcCluster_.password));
            }
            if (criteria.getDatabase() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDatabase(), JdbcCluster_.database));
            }
            if (criteria.getConfig() != null) {
                specification = specification.and(buildStringSpecification(criteria.getConfig(), JdbcCluster_.config));
            }
            if (criteria.getNodeId() != null) {
                specification = specification.and(buildSpecification(criteria.getNodeId(),
                    root -> root.join(JdbcCluster_.nodes, JoinType.LEFT).get(Node_.id)));
            }
        }
        return specification;
    }
}
